<%@page import="java.sql.DriverManager"%>
<%@page import="Configurations.OTSFile"%>
<%@page import="Configurations.AYFile"%>
<%@page import="java.util.ArrayList"%>
<%@page import="Configurations.MySQL"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%
    try {
        Connection con = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        Class.forName(MySQL.DB_DRIVER);
        con = DriverManager.getConnection(MySQL.DB_URL, MySQL.DB_USER, MySQL.DB_PASS);
        ArrayList<String> fieldOrderAy = AYFile.getAYFieldOrder();
        ArrayList<String> fieldOrderOts = OTSFile.getOTSFieldOrder();
        // Update Ay database
        String sql = "UPDATE " + MySQL.DB_APPS + " SET ";
        for (int i = 0; i < fieldOrderAy.size(); i++) {
            sql = sql + fieldOrderAy.get(i) + "=?, ";
        }
        sql = sql.substring(0, sql.length() - 2);
        sql = sql + " WHERE " + MySQL.DB_APPS_EMAIL + "=?";
        System.out.println(sql);
        ps = con.prepareStatement(sql);
        for (int i = 1; i <= fieldOrderAy.size(); i++) {
            ps.setString(i, request.getParameter(fieldOrderAy.get(i - 1)));
        }
        ps.setString(fieldOrderAy.size() + 1, request.getParameter(MySQL.DB_APPS_EMAIL));
        System.out.println(ps.toString());
        ps.execute();
        session.setAttribute("changes", "");

        con.close();
        rs.close();
        ps.close();
    } catch (Exception e) {
    }

    // Redirect if this page is being displayed by itself
    // Redirect back to index.jsp when changes are saved is complete
    if (request.getServletPath().equals("/saveChanges.jsp")) {
        response.sendRedirect("");
    }
%>